home *** CD-ROM | disk | FTP | other *** search
/ Interactive Web Graphics with Shout 3D / Interactive Web Graphics With Shout 3D.iso / pc / Shout3Ddemo / Shout3d_runtime / codebase / models / nodeSearchPathTests / Pyramid2.java < prev    next >
Text File  |  2000-10-20  |  3KB  |  114 lines

  1. /**    
  2.     Company:        Eyematic Interfaces
  3.     Project:        Shout3D 2.0 Sample Code
  4.     Class:            Pyramid
  5.     Date:            August 31, 1999
  6.     Description:    Pyramid geometry, subclass of IndexedFaceSet 
  7.     (C) Copyright Eyematic Interfaces, Inc. - 1997-2000 - All rights reserved
  8.  */
  9.  
  10. package models.nodeSearchPathTests;
  11.  
  12. import  shout3d.core.*;
  13.  
  14. /**
  15.  * Pyramid.
  16.  * 
  17.  * Subclass of IndexedFaceSet
  18.  * Adds three fields -- height width and depth.
  19.  * When any of these changes, the Pyramid recalculates the 
  20.  * vertices of its geometry.
  21.  * 
  22.  * @author Paul Isaacs
  23.  * @author Jim Stewartson
  24.  * @author Dave Westwood
  25.  */
  26.  
  27. public class Pyramid2 extends IndexedFaceSet implements FieldObserver {
  28.     
  29.     final public    FloatField    width  = new FloatField(this, "width",  Field.NON_NEGATIVE_FLOAT, 1);
  30.     final public    FloatField    height = new FloatField(this, "height", Field.NON_NEGATIVE_FLOAT, 1);
  31.     final public    FloatField    depth  = new FloatField(this, "depth",  Field.NON_NEGATIVE_FLOAT, 1);
  32.     
  33.     private float[] normalizedCoords = { -1, -1,  1,     1, -1,  1, 
  34.                                           1, -1, -1,    -1, -1, -1, 
  35.                                           0,  1,  0 };
  36.     private int[]   myCoordIndex  = {    0, 3, 2,  1, -1,  // base is a quad
  37.                                             0, 1, 4, -1, 
  38.                                         1, 2, 4, -1, 
  39.                                         2, 3, 4, -1, 
  40.                                         3, 0, 4, -1 };
  41.                              
  42.     
  43.     /**
  44.      * Constructs a default Pyramid
  45.      */
  46.     public Pyramid2(){
  47.         // Watch the 3 new fields to update vertices when they change
  48.         width.addFieldObserver(this, null);
  49.         height.addFieldObserver(this, null);
  50.         depth.addFieldObserver(this, null);
  51.         
  52.         // Set the IFS fields to create the initial geometry.
  53.         calculateVertices();
  54.         coordIndex.setValue(myCoordIndex);
  55.     }
  56.     
  57.     /**
  58.      *  Clean up. Subclasses must call this from within their own finalize() method
  59.      */
  60.     protected void finalize() throws Throwable { 
  61.         width.removeFieldObserver(this);
  62.         height.removeFieldObserver(this);
  63.         depth.removeFieldObserver(this);
  64.         super.finalize();
  65.     }
  66.     
  67.     /**
  68.      * 
  69.      * Subclasses must call this from within their own onFieldChange() method.
  70.      * 
  71.      */
  72.     public void onFieldChange(Field theField, Object userData) {
  73.         if ( theField == width || theField == height || theField == depth) {
  74.             calculateVertices();
  75.         }
  76.         else {
  77.             // Call the super class, it's not a field this node cares about.
  78.             super.onFieldChange(theField, userData);
  79.         }
  80.     }
  81.     
  82.     float[] cVals = null;
  83.     /**
  84.      * Calculates the values of the vertices based on the width/height/depth 
  85.      * fields.
  86.      */
  87.     public void calculateVertices() {
  88.         // Start with the coords in the field, reallocate if there are 
  89.         // the wrong amount.
  90.         Node testNode = coord.getValue();
  91.         Coordinate cNode = null;
  92.         if (testNode != null && testNode instanceof Coordinate)
  93.             cNode = (Coordinate) testNode;
  94.         else {
  95.             cNode = new Coordinate();
  96.             coord.setValue(cNode);
  97.         }
  98.         cVals = cNode.point.getValue();
  99.         if (cVals == null || cVals.length != normalizedCoords.length)
  100.             cVals = new float[normalizedCoords.length];
  101.  
  102.         // Scale the normalized coords by the fields to create the
  103.         // node's new coordinate values.
  104.         for (int i = 0; i < normalizedCoords.length-2; i += 3){
  105.             cVals[i]   = width.getValue()  * normalizedCoords[i];
  106.             cVals[i+1] = height.getValue() * normalizedCoords[i+1];
  107.             cVals[i+2] = depth.getValue()  * normalizedCoords[i+2];
  108.         }
  109.         
  110.         // Set the coord field with the new values
  111.         cNode.point.setValue(cVals);
  112.     }
  113. }
  114.